home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / stdfilterhacking / stdfilterhacking.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.3 KB  |  168 lines

  1. /*
  2.     File:        stdFilterHacking.c
  3.  
  4.     Contains:    For those of you who have been using the new Standard Dialog Filter
  5.                 in 7.0 and later (documented in tech note #304, which has been renamed 
  6.                 to MM.{something}.PendingUpdates) you may have encountered some odd behaviour
  7.                 when you bring up another dialog or alert in front of
  8.                 a dialog using the standard filter.
  9.  
  10.                 If you have dimmed (de-hilited, disabled) the OK or Cancel button and
  11.                 bring up a dialog on top, when you dismiss the top dialog the dialog
  12.                 where the OK button was dimmed will suddenly have the OK button enabled!
  13.  
  14.                 This is a behaviour of the standard filter.  When the standard filter
  15.                 gets an Activate event, it will automatically enable the default and
  16.                 cancel buttons (set with SetDialogDefaultItem and SetDialogCancelItem), it
  17.                 is assuming that the only reason those buttons were disabled was because they
  18.                 were in a deactivated window.
  19.  
  20.                 The filter has no state-saving routines or caches to remember what the old state
  21.                 of the buttons was, so it just slams them active again.
  22.  
  23.                 This may not be what you want.  If you disable an OK or Cancal button, use the
  24.                 standard filter, and bring another dialog or alert up on top of that dialog, 
  25.                 you need to add a little extra code in your own filter to handle that
  26.                 situation.
  27.  
  28.                 The code is included here, look in the filterIt function included in 
  29.                 stdFilterHacking.c.
  30.  
  31.                 Better Living Through Code.
  32.                 
  33.     Written by:     
  34.  
  35.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  36.  
  37.                 You may incorporate this Apple sample source code into your program(s) without
  38.                 restriction. This Apple sample source code has been provided "AS IS" and the
  39.                 responsibility for its operation is yours. You are not permitted to redistribute
  40.                 this Apple sample source code as "Apple sample source code" after having made
  41.                 changes. If you're going to re-distribute the source, we require that you make
  42.                 it clear in the source that the code was descended from Apple sample source
  43.                 code, but that you've made changes.
  44.  
  45.     Change History (most recent first):
  46.                 8/10/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  47.                 
  48.  
  49. */
  50. #include <Dialogs.h>
  51. #include <Controls.h>
  52. #include <QuickDraw.h>
  53. #include <Windows.h>
  54. #include <ToolUtils.h>
  55. #include <OSUtils.h>
  56. #include <Menus.h>
  57. #include <Fonts.h>
  58. #include <resources.h>
  59. #include <Sound.h>
  60. #include <Traps.h>
  61. #include <Gestaltequ.h>
  62. #include <Memory.h>
  63. #include <Scrap.h>
  64. #include <TextEdit.h>
  65.  
  66. /* prototypes */
  67. ControlHandle   SnatchHandle(DialogPtr thebox, short theGetItem);
  68. void            ToggleCheckBox(DialogPtr myDialog,short theItem);
  69. pascal     Boolean  filterIt(DialogPtr inputDialog, EventRecord *myDialogEvent, short *theDialogItem);
  70.  
  71.  
  72. enum  {
  73.     /* ok and cancel are already defined in Dialogs.h */
  74.     kMessUpItem = 3,
  75.     kHiliteOK
  76. };
  77. enum  {
  78.  
  79.     kSampleDialog = 128, 
  80.     kMessUpAlert
  81. };
  82.  
  83. ModalFilterUPP gModalFilterUPP;
  84.  
  85. void main(void)
  86. {
  87.     DialogPtr myDialog = nil;                               /* the dialog wee're using */
  88.     short hitItem = 0;                                      /* hitItem for ModalDialog */
  89.  
  90.     /* start up managers */
  91.     InitGraf(&qd.thePort);
  92.     InitFonts();
  93.     InitWindows();
  94.     InitMenus();
  95.     TEInit();
  96.     InitDialogs(nil);
  97.     InitCursor();
  98.     
  99.     /* set up the UPP for the Modal Dialog filter */
  100.     gModalFilterUPP = NewModalFilterProc(filterIt);        
  101.  
  102.     /* get our dialog */
  103.     myDialog = GetNewDialog(kSampleDialog, nil, (WindowPtr)-1);
  104.  
  105.     SetDialogDefaultItem(myDialog, ok);
  106.     SetDialogCancelItem(myDialog, cancel);
  107.     /* dim the OK button initially */
  108.     HiliteControl(SnatchHandle(myDialog,ok),255);
  109.  
  110.     do {
  111.         ModalDialog(gModalFilterUPP, &hitItem);
  112.         if(hitItem == kMessUpItem){
  113.         /* bring up an alert to obscure the OK button */
  114.         Alert(kMessUpAlert,nil);
  115.         }
  116.         else{
  117.         if(hitItem == kHiliteOK)
  118.         ToggleCheckBox(myDialog,kHiliteOK);        
  119.         HiliteControl(SnatchHandle(myDialog,ok),GetControlValue(SnatchHandle(myDialog,hitItem)) ? 0 : 255);
  120.         
  121.         }
  122.     
  123.     }
  124.     while (hitItem != ok && hitItem != cancel);
  125.    
  126.     DisposeDialog(myDialog);
  127.     InitCursor();                                           /* Init to prevent I-beam from hanging around in some circumstances */
  128.  
  129.  
  130. }
  131.  
  132.  
  133. pascal Boolean filterIt(DialogPtr inputDialog, EventRecord *myDialogEvent, short *theDialogItem)
  134. {
  135.    ModalFilterUPP  theModalProc;  /* address of standard filter */
  136.  
  137.  
  138.    if (/* myDialogEvent->what == activateEvt || */myDialogEvent->what == updateEvt) {
  139.    /* reset the state of the button based on my */
  140.    /* check box.  Use whatever conditions you need to determine the  */
  141.    /* correct highlighting */
  142.     HiliteControl(SnatchHandle(inputDialog,ok),GetControlValue(SnatchHandle(inputDialog,kHiliteOK)) ? 0 : 255);
  143.  
  144.     } 
  145. /* You must still call through the standard filter for things to work, of course */
  146.   GetStdFilterProc(&theModalProc);
  147.   return CallModalFilterProc(theModalProc, inputDialog, myDialogEvent, theDialogItem);
  148.  
  149. }
  150.  
  151. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  152. {
  153.     short itemtype;
  154.     Rect itemrect;
  155.     Handle thandle;
  156.     
  157.     GetDialogItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  158.     return((ControlHandle)thandle);
  159. } /* end SnatchHandle */
  160.  
  161.  
  162. void ToggleCheckBox(DialogPtr myDialog,short theItem)
  163. {
  164.  
  165. SetControlValue(SnatchHandle(myDialog,theItem),GetControlValue(SnatchHandle(myDialog,theItem)) ? false:true);
  166. }
  167.  
  168.